/*
 * @Author: 吴世扬 18368095041@163.com
 * @Date: 2024-08-21 10:26:28
 * @LastEditors: 吴世扬 18368095041@163.com
 * @LastEditTime: 2024-08-21 12:01:48
 * @FilePath: /vue3-uniapp/src/service/hook/useAfterAuthAndMescrollInit.ts
 */
import type {
    ViewInstance,
    OnInit
} from '@/components/ui/MescrollView/typings';
import { useForkJoin } from '@/service/rxjs/useForkJoin';
import { useAfterAuth } from '@/service/hook/useAfterAuth';

export const useAfterAuthAndMescrollInit = (
    fn: (option: AnyObject | undefined, viewInstance: ViewInstance) => void
) => {
    const mescrollInstance: ViewInstance = {
        endSuccess(page, pageSize, dataSize, totalSize) {
            excute1({
                page,
                pageSize,
                dataSize,
                totalSize,
                isError: false
            });
        },
        endErr() {
            excute1({
                page: undefined,
                pageSize: undefined,
                dataSize: undefined,
                totalSize: undefined,
                isError: true
            });
        }
    };
    const [excute1, excute2] = useForkJoin<
        {
            page?: number;
            pageSize?: number;
            dataSize?: number;
            totalSize?: number;
            isError: boolean;
        },
        ViewInstance
    >(([state, viewInstance]) => {
        if (!state.isError) {
            viewInstance.endSuccess(
                state.page,
                state.pageSize,
                state.dataSize,
                state.totalSize
            );
        } else {
            viewInstance.endErr();
        }
    });
    useAfterAuth(async (option) => {
        fn(option, mescrollInstance);
    });
    const onInit: OnInit = (viewInstance) => {
        excute2(viewInstance);
    };

    return {
        onMescrollInit: onInit
    };
};
